﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{

    /// <summary>
    /// 工作区管理器中的条目。
    /// </summary>
    public class WorkspaceTreeViewItem : TreeViewItem
    {
        static WorkspaceTreeViewItem()
        {
            fileBitmapImage = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/DocumentHS.png"));
            directoryBitmapImage = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/FolderHS.png"));
            pictureImage = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/PictureHS.png"));
            folderDocumentImage = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/FolderDocument.png"));
            imageFolderImage = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/ImageFolder.png"));
        }

        /// <summary>
        /// 图像文件图标。
        /// </summary>
        static BitmapImage pictureImage;
        /// <summary>
        /// Markdown 文件图标。
        /// </summary>
        static BitmapImage fileBitmapImage;
        /// <summary>
        /// Markdown 文件图标。
        /// </summary>
        public static BitmapImage FileBitmapImage
        {
            get { return fileBitmapImage; }
        }
        /// <summary>
        /// 目录（文件夹）图标。
        /// </summary>
        static BitmapImage directoryBitmapImage;
        /// <summary>
        /// 目录文件组合图标。
        /// </summary>
        static BitmapImage folderDocumentImage;
        /// <summary>
        /// 目录文件组合图标。
        /// </summary>
        public static BitmapImage FolderDocumentImage
        {
            get { return folderDocumentImage; }
        }

        /// <summary>
        /// 图像资源文件夹图标。
        /// </summary>
        static BitmapImage imageFolderImage;

        private Brush foregroundOfText = Brushes.Black;
        /// <summary>
        /// 文本前景色。
        /// </summary>
        public Brush ForegroundOfText { get { return foregroundOfText; } }

        /// <summary>
        /// [构造方法]工作区管理器条目。
        /// </summary>
        /// <param name="fullPath">指向的对象文件的完整磁盘路径。</param>
        /// <param name="masterWindow">主窗口。</param>
        public WorkspaceTreeViewItem(string fullPath, MainWindow masterWindow)
        {
            this.fullPath = fullPath;
            this.masterWindow = masterWindow;
            this.BorderThickness = new Thickness(1);
            this.Padding = new Thickness(2);
            this.SnapsToDevicePixels = true;
            this.showTitle = Globals.MainWindow.ShowTitleInWorkspaceManager;

            this.headerPanel.Children.Add(this.icon);

            this.headerPanel.Children.Add(this.headerTextBlock);
            this.Header = this.headerPanel;

            if (IsDirectoryExists)
            {
                headerTextBlock.FontWeight = FontWeights.Bold;

                if (IsImageResourceDirectory)
                {
                    headerTextBlock.Foreground = foregroundOfText = Brushes.DimGray;
                    this.icon.Source = imageFolderImage;
                }
                else
                {
                    headerTextBlock.Foreground = foregroundOfText = Brushes.Black;
                    this.icon.Source = directoryBitmapImage;

                    DirectoryInfo di = new DirectoryInfo(FullPath);
                    var metaFilePath = (di.FullName.EndsWith("\\") ? di.FullName : (di.FullName + "\\")) + "_" + di.Name + ".md";
                }

                if (fullPath == Globals.PathOfWorkspace)
                {
                    this.ToolTip = "工作区目录";
                }
                else
                {
                    this.ToolTip = this.FullPath;
                }
            }
            else if (IsImageFileExist)
            {
                this.icon.Source = pictureImage;
                this.ToolTip = "单击预览，双击在文档中添加引用";
            }
            else if (IsValidateFilePath)
            //if(IsMarkdownFilePath)
            {
                if (IsFolderDocument)
                {
                    this.icon.Source = folderDocumentImage;
                }
                else
                {
                    this.icon.Source = fileBitmapImage;
                }
                this.ToolTip = this.FullPath;
            }

            this.headerTextBlock.Inlines.Add(fileStatusSpan);
            this.headerTextBlock.Inlines.Add(splitterSpan);
            this.headerTextBlock.Inlines.Add(titleOrShortNameSpan);

            //this.headerTextBlock.Inlines.Add(new Span(new Run(this.ShortName)));
            //改成从文件中读取标题文本。
            RefreshFileState(); //含调用ShowHeaderText();
            //this.headerTextBlock.Foreground = this.Foreground;
            //Binding foregroundBinding = new Binding() { Source = this, Path = new PropertyPath(TreeViewItem.ForegroundProperty), };
            //this.SetBinding(TextBlock.ForegroundProperty, foregroundBinding);

            this.headerPanel.PreviewMouseLeftButtonDown += headerPanel_PreviewMouseLeftButtonDown;
            this.headerPanel.PreviewMouseDown += headerPanel_PreviewMouseDown;
        }

        /// <summary>
        /// 根据 “ShowTitle”刷新。如果“ShowTitle”为真，尝试显示文档中设置的文档标题；否则直接显示文件短名。
        /// </summary>
        private void ShowHeaderText()
        {
            this.titleOrShortNameSpan.Inlines.Clear();

            if (showTitle)
            {
                if (this.IsMarkdownFilePath)
                {
                    var title = MainWindow.GetTitleOfMdFile(this.fullPath);
                    if (string.IsNullOrWhiteSpace(title) == false)
                    {
                        this.titleOrShortNameSpan.Inlines.Add(new Run(title));
                    }
                    else
                    {
                        this.titleOrShortNameSpan.Inlines.Add(new Run(this.ShortName));
                    }
                }
                else if (this.IsDirectoryExists)
                {
                    DirectoryInfo di = new DirectoryInfo(this.fullPath);
                    var metaFilePath = (di.FullName.EndsWith("\\") ? di.FullName : (di.FullName + "\\")) + "_" + di.Name + ".md";
                    if (File.Exists(metaFilePath))
                    {
                        var metaFileTitle = MainWindow.GetTitleOfMdFile(metaFilePath);
                        if (string.IsNullOrWhiteSpace(metaFilePath) == false)
                        {
                            this.titleOrShortNameSpan.Inlines.Add(new Run(metaFileTitle));
                        }
                        else
                        {
                            this.titleOrShortNameSpan.Inlines.Add(new Run(this.ShortName));
                        }
                    }
                    else
                    {
                        this.titleOrShortNameSpan.Inlines.Add(new Run(this.ShortName));
                    }
                }
                else
                {
                    this.titleOrShortNameSpan.Inlines.Add(new Run(this.ShortName));
                }
            }
            else
            {
                this.titleOrShortNameSpan.Inlines.Add(new Run(this.ShortName));
            }
        }

        private bool showTitle = false;
        /// <summary>
        /// 为真时尝试显示在文件中设置的文档标题。否则直接显示文件短名。
        /// </summary>
        public bool ShowTitle
        {
            get { return showTitle; }
            set
            {
                showTitle = value;
                ShowHeaderText();
            }
        }

        /// <summary>
        /// 更改选取状态。
        /// </summary>
        void headerPanel_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.IsSelected = true;
        }

        /// <summary>
        /// 双击当前项触发不同操作：
        /// 　　如果是 Markdown 文件，打开；
        /// 　　如果是图像文件，预览并作为链接插入到当前活动编辑器中；
        /// 　　如果是目录，打开或创建目录元文件。
        /// </summary>
        void headerPanel_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (IsMarkdownFilePath)
                {
                    OpenFile();
                    e.Handled = true;
                }
                else if (IsValidateImageFilePath(this.fullPath))
                {
                    PreviewImage();
                    InsertImageTagToDocument(this.fullPath);
                    e.Handled = true;
                }
                else if (IsDirectoryExists)
                {
                    OpenOrCreateDirectoryMetaFile();
                    e.Handled = true;
                }
            }
            else if (e.ClickCount == 1)
            {
                PreviewImage();
            }
        }

        /// <summary>
        /// 打开或创建目录元文件。
        /// </summary>
        private void OpenOrCreateDirectoryMetaFile()
        {
            if (this.masterWindow != null)
            {
                string[] files = new string[1];
                var di = new DirectoryInfo(this.FullPath);
                if (di.Name.EndsWith("~"))
                {
                    LMessageBox.Show("　　以波型符结尾的是资源文件夹，不允许创建对应 Markdown 文件。", Globals.AppName,
                          MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                files[0] = di.FullName + "_" + di.Name + ".md";

                try
                {
                    if (System.IO.File.Exists(files[0]) == false)
                    {
                        File.WriteAllText(files[0], $"\r\n%{MainWindow.FormatDocumentTitle(di.Name)}\r\n\r\n；{DateTime.Now.ToString()}");
                    }

                    this.masterWindow.OpenDocuments(files);

                    var workdpaceEntryItem = this.masterWindow.FindWorkspaceTreeViewItem(di.FullName);
                    if (workdpaceEntryItem != null) workdpaceEntryItem.RefreshFileState();
                }
                catch (Exception ex)
                {
                    LMessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
        }

        /// <summary>
        /// 向当前活动编辑器中添加对图像文件的引用。
        /// 根据图像文件是否在工作区决定是否将图像文件复制到工作区目录下。
        /// </summary>
        /// <param name="imageFilePath">图像文件路径。</param>
        /// <param name="noRefreshWorkspaceManager">是否不需要刷新主界面工作区管理器。</param>
        public void InsertImageTagToDocument(string imageFilePath, bool noRefreshWorkspaceManager = false)
        {
            //在当前文档中添加对当前图片的引用。
            if (File.Exists(imageFilePath) == false)
            {
                return;
            }

            //如果该图片不在当前文件资源文件夹下，先复制，然后再添加。
            var selEditor = Globals.MainWindow.ActivedEditor;
            if (selEditor == null) return;
            if (File.Exists(selEditor.FullFilePath) == false)
            {
                LMessageBox.Show("　　需要先保存正在编辑的文件才能使用这个功能。", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            var curDocumentImageResourceFolder = selEditor.ImageResourceDirectoryPath;

            if (Directory.Exists(curDocumentImageResourceFolder) == false)
            {
                Directory.CreateDirectory(curDocumentImageResourceFolder);

                if (noRefreshWorkspaceManager == false)
                {
                    Globals.MainWindow.WorkspaceManager.Refresh(null);
                }
            }

            //这里要注意：
            //在工作区内的直接添加引用，不在工作区的才复制文件。
            selEditor.TryToDropImages(new System.Collections.Specialized.StringCollection() { FullPath, });
        }

        /// <summary>
        /// 取当前项对应的资源文件夹的完整路径。
        /// </summary>
        public string ResourceDirectoryFullPath
        {
            get
            {
                if (IsMarkdownFilePath == false) return string.Empty;

                var parentDirectoryPath = this.fullPath.Substring(0, this.fullPath.LastIndexOf("\\") + 1);

                return parentDirectoryPath + ShortName.Substring(0, ShortName.Length - 3) + "~";
            }
        }

        /// <summary>
        /// 打开当前项对应的 Markdown 文件。
        /// </summary>
        public void OpenFile()
        {
            if (this.IsDirectoryExists) return;//双击目录不进行其它操作，默认会展开，不必写代码。

            if (this.masterWindow != null)
            {
                string[] files = new string[1];
                files[0] = this.fullPath;

                if (System.IO.File.Exists(this.fullPath) == false)
                {
                    LMessageBox.Show("　　未找到此文档。可能通过其它途径（例如在Windows资源管理器中）删除了对应的磁盘文件。\r\n　　下次启动此程序时将不会再显示这个条目。",
                        Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                if (this.IsMarkdownFilePath)
                {
                    this.masterWindow.OpenDocuments(files);
                }
            }
        }

        /// <summary>
        /// 预览当前项指向的图像文件。
        /// </summary>
        public void PreviewImage()
        {
            if (this.IsDirectoryExists) return;//双击目录不进行其它操作，默认会展开，不必写代码。
            if (this.masterWindow != null)
            {
                string[] files = new string[1];
                files[0] = this.fullPath;

                if (System.IO.File.Exists(this.fullPath) == false)
                {
                    LMessageBox.Show("　　未找到此文档。可能通过其它途径（例如在Windows资源管理器中）删除了对应的磁盘文件。\r\n　　下次启动此程序时将不会再显示这个条目。",
                        Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                if (this.IsImageFileExist)
                {
                    try
                    {
                        //this.masterWindow.ImagePreview.Source = new BitmapImage(new Uri(this.fullPath));//不能加此前缀"file:///" + 
                        //用上面这个办法会造成图片文件无法删除，因为会被当前进程占用。
                        using (BinaryReader reader = new BinaryReader(File.Open(this.fullPath, FileMode.Open)))
                        {
                            FileInfo fi = new FileInfo(this.fullPath);
                            byte[] bytes = reader.ReadBytes((int)fi.Length);
                            reader.Close();

                            var bitmapImage = new BitmapImage();
                            bitmapImage.BeginInit();
                            bitmapImage.StreamSource = new MemoryStream(bytes);
                            bitmapImage.EndInit();
                            Globals.MainWindow.ImagePreview.Source = bitmapImage;
                            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                        }

                        this.masterWindow.imagePreviewOutBorder.Tag = this;
                        Globals.MainWindow.tcLeftTools.SelectedIndex = 0;

                        //自动弹出图像预览区域并不好，容易导致用户找不到当前选中的项目。
                        //if (Globals.MainWindow.rdLeftTools.ActualHeight < 40)
                        //{
                        //    Globals.MainWindow.rdLeftTools.Height = new GridLength(300);
                        //}
                    }
                    catch (Exception ex)
                    {
                        LMessageBox.Show(ex.Message, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }
            }
        }

        /// <summary>
        /// 承载当前项名称文本、图标的 StackPanel。
        /// </summary>
        private StackPanel headerPanel = new StackPanel()
        {
            Orientation = Orientation.Horizontal,
            Margin = new Thickness(4, 0, 4, 0),
        };

        /// <summary>
        /// 用于显示文件完成状态标记。
        /// </summary>
        private Span fileStatusSpan = new Span();

        /// <summary>
        /// 用来分隔“文件完成状态标记”与“文件标题（或短名）标记”。
        /// </summary>
        private Span splitterSpan = new Span();

        /// <summary>
        /// 用以显示文档标题（或文件短名）。
        /// </summary>
        private Span titleOrShortNameSpan = new Span();

        private TextBlock headerTextBlock = new TextBlock() { Foreground = Brushes.Black, };
        /// <summary>
        /// 用于显示文件短名。
        /// </summary>
        public TextBlock HeaderTextBlock { get { return headerTextBlock; } }

        /// <summary>
        /// 用于显示条目类别图标。
        /// </summary>
        private Image icon = new Image() { Width = 16, Height = 16, Margin = new Thickness(0, 0, 2, 0), };

        /// <summary>
        /// IsMarkdownFilePath的别名。
        /// </summary>
        public bool IsValidateFilePath
        {
            get { return IsMarkdownFilePath; }
        }

        /// <summary>
        /// 是否合法的文本文件的路径。
        /// </summary>
        public bool IsMarkdownFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(this.fullPath)) return false;

                if (File.Exists(this.fullPath) == false) return false;

                if (this.fullPath.ToLower().EndsWith(".md") == false) return false;

                if (this.fullPath.Length <= 4) return false;

                return true;
            }
        }

        /// <summary>
        /// 是不是所在目录指向的特定文件，规则是：“_目录名.md”（“_目录名.html”）
        /// </summary>
        public bool IsFolderDocument
        {
            get
            {
                if (IsMarkdownFilePath == false) return false;

                if (File.Exists(this.fullPath) == false) return false;

                FileInfo fileInfo = new FileInfo(this.fullPath);
                if (fileInfo.Name.ToLower() == ("_" + fileInfo.Directory.Name + ".md").ToLower())
                {
                    return true;
                }

                return false;
            }
        }

        private string fullPath;
        /// <summary>
        /// 当前项指向的 Markdown 文件、图像文件、目录的完全路径。
        /// </summary>
        public string FullPath
        {
            get
            {
                if (IsDirectoryExists && this.fullPath.EndsWith("\\") == false)
                {
                    this.fullPath += "\\";
                }
                return this.fullPath;
            }
            set
            {
                this.fullPath = value;
                this.headerTextBlock.Text = this.ShortName;
            }
        }

        /// <summary>
        /// 当前项指向的 Markdown 文件、图像文件、目录的短名。
        /// </summary>
        public string ShortName
        {
            get
            {
                if (string.IsNullOrEmpty(this.fullPath))
                {
                    return string.Empty;
                }

                if (this.fullPath.EndsWith("\\"))//目录
                {
                    if (this.fullPath.Length < 2) return string.Empty;

                    int index = this.fullPath.LastIndexOf('\\', this.fullPath.Length - 2);//不取最后一个
                    if (index < 0) return this.fullPath.Substring(0, this.fullPath.Length - 1);

                    return this.fullPath.Substring(index + 1, this.fullPath.Length - index - 2);//不含反斜杠。
                }
                else
                {
                    int lastIndex = this.fullPath.LastIndexOf('\\');
                    if (lastIndex < 0) return this.fullPath;

                    return this.fullPath.Substring(lastIndex + 1);
                }
            }
        }

        /// <summary>
        /// 当前项指向的对象是否有效目录。
        /// </summary>
        public bool IsDirectoryExists
        {
            get { return Directory.Exists(this.fullPath); }
        }

        /// <summary>
        /// 当前项指向的对象是否有效的资源文件夹。
        /// </summary>
        public bool IsImageResourceDirectory
        {
            get
            {
                if (IsDirectoryExists == false) return false;

                DirectoryInfo directoryInfo = new DirectoryInfo(this.fullPath);
                if (directoryInfo.Name == "Images~") return true;

                return false;
            }
        }

        /// <summary>
        /// 当前项指向的对象是否有效的图像资源文件。
        /// </summary>
        public bool IsImageFileExist
        {
            get
            {
                var b = IsValidateImageFilePath(this.fullPath);
                return b;
            }
        }

        /// <summary>
        /// 指定路径是否有效的图像文件路径。
        /// </summary>
        /// <param name="fullPath">要检查的图像文件路径。</param>
        public static bool IsValidateImageFilePath(string fullPath)
        {
            if (File.Exists(fullPath) == false) return false;
            var path = fullPath.ToLower();

            if (path.EndsWith(".bmp")) return true;
            else if (path.EndsWith(".ico")) return true;//可以支持
            else if (path.EndsWith(".png")) return true;
            else if (path.EndsWith(".gif")) return true;
            else if (path.EndsWith(".jpg")) return true;
            else if (path.EndsWith(".jpeg")) return true;
            else if (path.EndsWith(".tiff")) return true;

            return false;
        }

        /// <summary>
        /// 当前项指向的路径是否存在有效的文件。
        /// </summary>
        public bool IsFileExists
        {
            get { return File.Exists(this.fullPath); }
        }

        /// <summary>
        /// 当前项指向的路径是否有效的资源文件。
        /// </summary>
        public bool IsResourceFile
        {
            get
            {
                if (File.Exists(this.fullPath) == false) return false;

                int lastIndex = this.fullPath.LastIndexOf("\\");
                if (lastIndex < 0) return false;

                var s = this.fullPath.Substring(0, lastIndex);
                if (s.EndsWith("~") == false) return false;

                return true;
            }
        }

        /// <summary>
        /// 当前项指向的目录是否有效的资源目录。
        /// </summary>
        public bool IsResourceDirectory
        {
            get
            {
                if (Directory.Exists(this.fullPath) == false) return false;

                if (this.fullPath.EndsWith("~") == false) return false;

                return true;
            }
        }

        private MainWindow masterWindow;
        /// <summary>
        /// 要用来打开文档，故需要保持此信息。
        /// </summary>
        public MainWindow MasterWindow { get { return this.masterWindow; } }

        /// <summary>
        /// 当前项指向的对象所在的上级目录。
        /// </summary>
        public string ParentDirectory
        {
            get
            {
                if (IsMarkdownFilePath)
                {
                    return this.fullPath.Substring(0, this.fullPath.LastIndexOf("\\") + 1);
                }
                else if (IsDirectoryExists)
                {
                    var path = this.fullPath;
                    if (path.EndsWith("\\")) path = path.Substring(0, path.Length - 1);
                    int lastIndex = path.LastIndexOf("\\");
                    if (lastIndex < 0) return null;

                    return path.Substring(0, lastIndex + 1);
                }

                return null;
            }
        }

        /// <summary>
        /// 当前项的父项。
        /// </summary>
        public WorkspaceTreeViewItem ParentWorkspaceTreeViewItem
        {
            get
            {
                if (this.Parent == null) return null;
                var parent = this.Parent as WorkspaceTreeViewItem;
                return parent;
            }
        }

        /// <summary>
        /// 刷新当前 Markdown 或目录元文件的完成状态标志和标题（如果 Globals.MainWindow.ShowTitleInWorkspaceManager为true）。
        /// </summary>
        internal void RefreshFileState()
        {
            this.ShowHeaderText();

            var destFilePath = fullPath;

            if (File.Exists(fullPath) == false)
            {
                if (Directory.Exists(fullPath) == false)
                {
                    this.headerTextBlock.Inlines.Add(new Span(new Run(this.ShortName)));
                    return;
                }
                else
                {
                    DirectoryInfo di = new DirectoryInfo(fullPath);
                    var metaFilePath = (di.FullName.EndsWith("\\") ? di.FullName : (di.FullName + "\\")) + "_" + di.Name + ".md";
                    if (File.Exists(metaFilePath) == false)
                    {
                        this.fileStatusSpan.Inlines.Clear();

                        //实际上后一个是根本不会显示在工作区的。因为路径是这样的：ResourceDirectory~/Images~。
                        if (this.IsImageResourceDirectory || this.IsResourceDirectory)
                        {
                            this.fileStatusSpan.Inlines.Add(new Run(""));
                        }
                        else
                        {
                            this.fileStatusSpan.Inlines.Add(new Run("[-]"));
                        }

                        this.fileStatusSpan.Background = Brushes.Red;
                        this.fileStatusSpan.Foreground = Brushes.White;
                        this.fileStatusSpan.FontWeight = FontWeights.Bold;
                        this.fileStatusSpan.ToolTip = "未开始";

                        this.splitterSpan.Inlines.Clear();
                        this.splitterSpan.Inlines.Add(new Run(" "));
                        return;
                    }

                    destFilePath = metaFilePath;
                }
            }

            using (StreamReader sr = new StreamReader(destFilePath))
            {
                var fstLine = sr.ReadLine();
                if (fstLine != null && fstLine.Length >= 3)
                {
                    string header = "";
                    Regex regex = new Regex(@"\[[ 　\t]*[－\-\+＋\#＃\%％][ 　\t]*\]");
                    var match = regex.Match(fstLine);
                    if (match.Success) header = match.Value.Replace("－", "-").Replace("＋", "+").Replace("％", "%").Replace("＃", "#").Replace(" ", "").Replace("　", "").Replace("\t", "");

                    switch (header)
                    {
                        case "[%]":
                            {
                                this.fileStatusSpan.Inlines.Clear();
                                this.fileStatusSpan.Inlines.Add(new Run(header));
                                this.fileStatusSpan.Background = Brushes.Green;
                                this.fileStatusSpan.Foreground = Brushes.White;
                                this.fileStatusSpan.FontWeight = FontWeights.Bold;
                                this.fileStatusSpan.ToolTip = "未完成...";

                                this.splitterSpan.Inlines.Clear();
                                this.splitterSpan.Inlines.Add(new Run(" "));
                                break;
                            }
                        case "[+]":
                            {
                                this.fileStatusSpan.Inlines.Clear();
                                this.fileStatusSpan.Inlines.Add(new Run(header));
                                this.fileStatusSpan.Background = Brushes.Blue;
                                this.fileStatusSpan.Foreground = Brushes.White;
                                this.fileStatusSpan.FontWeight = FontWeights.Bold;
                                this.fileStatusSpan.ToolTip = "定稿";

                                this.splitterSpan.Inlines.Clear();
                                this.splitterSpan.Inlines.Add(new Run(" "));
                                break;
                            }
                        case "[#]":
                            {
                                this.fileStatusSpan.Inlines.Clear();
                                this.fileStatusSpan.Inlines.Add(new Run(header));
                                this.fileStatusSpan.Background = Brushes.Brown;
                                this.fileStatusSpan.Foreground = Brushes.White;
                                this.fileStatusSpan.FontWeight = FontWeights.Bold;
                                this.fileStatusSpan.ToolTip = "废弃";

                                this.splitterSpan.Inlines.Clear();
                                this.splitterSpan.Inlines.Add(new Run(" "));
                                break;
                            }
                        //case "[-]":
                        default:
                            {
                                this.fileStatusSpan.Inlines.Clear();
                                this.fileStatusSpan.Inlines.Add(new Run(header));
                                this.fileStatusSpan.Background = Brushes.Red;
                                this.fileStatusSpan.Foreground = Brushes.White;
                                this.fileStatusSpan.FontWeight = FontWeights.Bold;
                                this.fileStatusSpan.ToolTip = "未开始";

                                this.splitterSpan.Inlines.Clear();
                                this.splitterSpan.Inlines.Add(new Run(" "));
                                break;
                            }
                    }
                }
                else
                {
                    this.fileStatusSpan.Inlines.Clear();
                    this.fileStatusSpan.Inlines.Add(new Run("[-]"));
                    this.fileStatusSpan.Background = Brushes.Red;
                    this.fileStatusSpan.Foreground = Brushes.White;
                    this.fileStatusSpan.FontWeight = FontWeights.Bold;
                    this.fileStatusSpan.ToolTip = "未开始";

                    this.splitterSpan.Inlines.Clear();
                    this.splitterSpan.Inlines.Add(new Run(" "));
                }
            }
        }
    }

    /// <summary>
    /// 按名称对工作区管理器中的同级条目进行排序。
    /// </summary>
    public class WorkspaceTreeViewItemCompare : IComparer<WorkspaceTreeViewItem>
    {
        public int Compare(WorkspaceTreeViewItem x, WorkspaceTreeViewItem y)
        {
            return x.ShortName.CompareTo(y.ShortName);
        }
    }
}
